<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
      class="el-forms"
    >
      <div class="handle_box_content" ref="handle_box_content">
        <el-form-item label="任务标准" prop="projectId">
          <el-input
            v-model="queryParams.workStandards"
            placeholder="请输入任务标准"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="编码" prop="projectName">
          <el-input
            v-model="queryParams.code"
            placeholder="请输入编码"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="标准化" prop="SORT">
          <el-input
            v-model="queryParams.standard"
            placeholder="请输入标准化"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="标准化编号" prop="batchCode">
          <el-input
            v-model="queryParams.number1"
            placeholder="请输入标准化编号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="安全性评价" prop="toolName">
          <el-input
            v-model="queryParams.safetyEvaluation"
            placeholder="请输入安全性评价"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="评价编号" prop="kindNumber">
          <el-input
            v-model="queryParams.number2"
            placeholder="请输入评价编号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="责任制落实" prop="FACTORY">
          <el-input
            v-model="queryParams.responsibilities"
            placeholder="请输入责任制落实"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="落实编号" prop="custodyPerson">
          <el-input
            v-model="queryParams.number3"
            placeholder="请输入落实编号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="体系评价" prop="managePerson">
          <el-input
            v-model="queryParams.systemEvaluate"
            placeholder="请输入体系评价"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="评价编号" prop="results">
          <el-input
            v-model="queryParams.number4"
            placeholder="请输入评价编号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="工作频次" prop="REMARK">
          <el-select
            v-model="queryParams.workFrequency"
            placeholder="请输入工作频次"
            clearable
            @keyup.enter.native="handleQuery"
          >
            <el-option
              v-for="item in optionItem"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="责任岗位" prop="REMARK">
          <el-input
            v-model="queryParams.responsiblePost"
            placeholder="请输入责任岗位"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="责任人" prop="REMARK">
          <el-input
            v-model="queryParams.responsiblePerson"
            placeholder="请输入责任人"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
      </div>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
        <el-button
          plain
          size="mini"
          :icon="Showbox ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"
          @click="handleShow"
          >{{ Showbox ? "隐藏" : "展开" }}</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:dictionary:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAddZi"
          v-hasPermi="['system:dictionary:add']"
          >新增子记录</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:dictionary:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:dictionary:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:dictionary:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      ref="tables"
      v-if="refreshTable"
      v-loading="loading"
      :data="menuList"
      stripe
      row-key="id"
      :default-expand-all="false"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      @row-click="rowClick"
      @selection-change="handleSelectionChange"
      @select-all="selectAll"
      @select="select"
      border
    >
      <el-table-column type="selection" width="55" />
      <el-table-column
        prop="workStandards"
        label="工作任务标准"
        :show-overflow-tooltip="true"
        width="160"
      >
        <template slot-scope="scope">
          <i
            v-if="scope.row.children == undefined"
            @click="rowClick(scope.row)"
            style="font-size: 12px"
            class="el-icon-arrow-right"
          ></i>
          {{ scope.row.workStandards }}
        </template>
      </el-table-column>
      <el-table-column label="编码" align="center" prop="code" />
      <el-table-column label="标准化" align="center" prop="standard" />
      <el-table-column label="编号" align="center" prop="number1" />
      <el-table-column
        label="安全性评价"
        align="center"
        prop="safetyEvaluation"
      />
      <el-table-column label="编号" align="center" prop="number2" />
      <el-table-column
        label="责任制落实"
        align="center"
        prop="responsibilities"
      />
      <el-table-column label="编号" align="center" prop="number3" />
      <el-table-column label="体系评价" align="center" prop="systemEvaluate" />
      <el-table-column label="编号" align="center" prop="number4" />
      <el-table-column label="工作频次" align="center" prop="workFrequency" />
      <el-table-column label="责任部门" align="center" prop="responsibleDep" />
      <el-table-column label="责任岗位" align="center" prop="responsiblePost" />
      <el-table-column label="责任人" align="center" prop="responsiblePerson" />
      <el-table-column label="标准分" align="center" prop="standardScore" />
      <el-table-column
        label="操作"
        align="center"
        width="150"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:manger:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:manger:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  listDictionary,
  queryOne,
  getDictionary,
  addDictionary,
  updateDictionary,
  delDictionary,
} from "../../../api/system/review/dictionar";
import PeopleSelect from "../../../components/Selection/index.vue";
import DepartmentDialog from "../../../components/DepartmentDialog/index.vue";
export default {
  name: "Manger",
  dicts: ["sys_normal_disable"],
  components: {
    PeopleSelect,
    DepartmentDialog,
  },
  data() {
    return {
      refreshTable: true,
      menuList: [],
      optionItem: [
        {
          lable: "日",
          value: "日",
        },
        {
          lable: "周",
          value: "周",
        },
        {
          lable: "月",
          value: "月",
        },
        {
          lable: "季度",
          value: "季度",
        },
        {
          lable: "年",
          value: "年",
        },
      ],
      // 按钮loading
      // buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      selectedData: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      mangerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        id: undefined,
        parentId: undefined,
        workStandards: undefined,
        code: undefined,
        standard: undefined,
        number1: undefined,
        safetyEvaluation: undefined,
        number2: undefined,
        responsibilities: undefined,
        number3: undefined,
        systemEvaluate: undefined,
        number4: undefined,
        workFrequency: undefined,
        responsibleDep: undefined,
        responsiblePost: undefined,
        responsiblePerson: undefined,
        standardScore: undefined,
        createCode: undefined,
        softwareExecution: undefined,
        isSoftware: undefined,
        isDelete: undefined,
        softwareExecutionMenu: undefined,
        reviewProject: undefined,
        securityStandardScore: undefined,
        responsibilitiesScore: undefined,
        systemScore: undefined,
        level: undefined,
        parentId: 0,
      },
      // 表单参数
      form: {},
      Showbox: false,
      peopleOpen: false,
      peopleOpen1: false,
      // 表单校验
      rules: {
        ID: [{ required: true, message: "主键id不能为空", trigger: "blur" }],
        refId: [{ required: true, message: "外键不能为空", trigger: "blur" }],
        projectId: [
          { required: true, message: "项目id不能为空", trigger: "blur" },
        ],
        projectName: [
          { required: true, message: "项目名称不能为空", trigger: "blur" },
        ],
        SORT: [{ required: true, message: "类别不能为空", trigger: "blur" }],
        batchCode: [
          { required: true, message: "批次编号不能为空", trigger: "blur" },
        ],
        toolName: [
          { required: true, message: "器具名称不能为空", trigger: "blur" },
        ],
        kindNumber: [
          { required: true, message: "种类序列号不能为空", trigger: "blur" },
        ],
        TYPE: [{ required: true, message: "型号不能为空", trigger: "change" }],
        FACTORY: [
          { required: true, message: "制造厂家不能为空", trigger: "blur" },
        ],
        custodyPerson: [
          { required: true, message: "保管负责人不能为空", trigger: "blur" },
        ],
        startTime: [
          { required: true, message: "开始时间不能为空", trigger: "blur" },
        ],
        endTime: [
          { required: true, message: "结束时间不能为空", trigger: "blur" },
        ],
        managePerson: [
          { required: true, message: "管理责任人不能为空", trigger: "blur" },
        ],
        RESULTS: [
          { required: true, message: "检验结果不能为空", trigger: "blur" },
        ],
        inspectDate: [
          { required: true, message: "检验日期不能为空", trigger: "blur" },
        ],
        nextInspectDate: [
          { required: true, message: "下次检验日期不能为空", trigger: "blur" },
        ],
        REMARK: [{ required: true, message: "备注不能为空", trigger: "blur" }],
        FILE: [
          { required: true, message: "附件信息不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    selectAll(selection) {},
    select(selection, row) {},
    open1() {
      this.peopleOpen = true;
    },
    open2() {
      this.peopleOpen1 = true;
    },
    //选择人的确定按钮事件 submitPeople(nikeNamelist)方法传参一个默认接收用户昵称数组   submitPeople(peopleList,nikeNamelist)方法传参两个则是接收用户昵称数组和用户账号数组
    submitPeople(peopleList) {
      this.peopleOpen = false;
    },
    submitPeople1(peopleList) {
      this.peopleOpen1 = false;
    },
    handleShow() {
      this.Showbox = !this.Showbox;
      const element = this.$refs.handle_box_content;
      if (this.Showbox) {
        element.style.overflow = "visible";
        element.style.height = "fit-content";
      } else {
        element.style.overflow = "hidden";
        element.style.height = "50px";
      }
    },
    /** 查询工器具管理列表 */
    async getList(row) {
      this.loading = true;
      await queryOne(this.queryParams).then(async (response) => {
        if (row != "" && row != undefined) {
          console.log(row.children);
          this.$refs.tables.toggleRowExpansion(row);
          const secondLevelData = await queryOne({
            parentId: row.id,
          });
          this.$refs.tables.toggleRowExpansion(row);
          row.children = this.handleTree(secondLevelData, "id");
        } else {
          const topLevelData = await queryOne(this.queryParams);
          const topLevelNodes = this.handleTree(topLevelData, "id");
          for (let i = 0; i < topLevelData.length; i++) {
            const secondLevelData = await queryOne({
              parentId: topLevelData[i].id,
            });
            const secondLevelNodes = this.handleTree(secondLevelData, "id");
            topLevelNodes[i].children = secondLevelNodes;
          }
          this.menuList = topLevelNodes;
        }
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: "",
        parentId: "",
        workStandards: "",
        code: "",
        standard: "",
        number1: "",
        safetyEvaluation: "",
        number2: "",
        responsibilities: "",
        number3: "",
        systemEvaluate: "",
        number4: "",
        workFrequency: "",
        responsibleDep: "",
        responsiblePost: "",
        responsiblePerson: "",
        standardScore: "",
        createCode: "",
        softwareExecution: "",
        isSoftware: "",
        isDelete: "",
        softwareExecutionMenu: "",
        reviewProject: "",
        securityStandardScore: "",
        responsibilitiesScore: "",
        systemScore: "",
        level: "",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.parentId = 0;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      //清空查询条件
      this.queryParams.workStandards = "";
      this.queryParams.code = "";
      this.queryParams.standard = "";
      this.queryParams.number1 = "";
      this.queryParams.safetyEvaluation = "";
      this.queryParams.number2 = "";
      this.queryParams.responsibilities = "";
      this.queryParams.number3 = "";
      this.queryParams.systemEvaluate = "";
      this.queryParams.number4 = "";
      this.queryParams.workFrequency = "";
      this.queryParams.responsiblePost = "";
      this.queryParams.responsiblePerson = "";
      this.queryParams.number4 = "";
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      // this.selectedData = selection;
      // this.ids = selection.map((item) => item.id);
      // this.single = selection.length !== 1;
      // this.multiple = !selection.length;
      this.ids = []; // 重置选中的ids
      this.flattenSelection(selection); // 调用递归函数
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    flattenSelection(nodes) {
      nodes.forEach((node) => {
        this.ids.push(node.id); // 收集当前节点的id
        // if (node.children && node.children.length) {
        //   this.flattenSelection(node.children); // 递归处理子节点
        // }
      });
    },
    rowClick(row) {
      this.queryParams.parentId = row.id;
      this.getList(row);
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({
        path: "/review/basicdata/dataDictionaryForm",
        query: { title: "新增数据字典" },
      });
    },
    /** 新增子记录按钮操作 */
    handleAddZi() {
      this.$router.push({
        path: "/review/basicdata/dataDictionaryForm",
        query: {
          title: "新增数据字典子数据",
          // id: this.selectedData[0].id,
          // level: this.selectedData[0].level,
          vercarl: "1",
        },
      });
    },
    /** 数据字典按钮操作 */
    distribute() {
      $.modal.open("title", "url", 100, 100, callback);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // this.loading = true;
      this.reset();
      const ID = row.id || this.ids;
      getDictionary(ID).then((response) => {});

      this.$router.push({
        path: "/review/basicdata/dataDictionaryForm",
        query: { ID: ID, title: "修改数据字典", flowId: 1 },
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          // this.buttonLoading = true;
          if (this.form.ID != null) {
            updateManger(this.form)
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                // this.buttonLoading = false;
              });
          } else {
            addManger(this.form)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                // this.buttonLoading = false;
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const IDs = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除数据字典编号为"' + IDs + '"的数据项？')
        .then(() => {
          this.loading = true;
          return delDictionary(IDs);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {})
        .finally(() => {
          this.loading = false;
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.queryParams = undefined;
      this.download(
        "system/dictionary/export",
        {
          ...this.queryParams,
        },
        `data_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style>
.handle_box_content {
  width: 81%;
  height: 50px;
  overflow: hidden;
}
.el-forms {
  display: flex;
  flex-wrap: nowrap;
  justify-content: space-between;
}
</style>
